//
//  ForgetEmailViewController.m
//  project
//
//  Created by 黄建华 on 2018/6/14.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "ForgetEmailViewController.h"
#import "ForgetViewController.h"

@interface ForgetEmailViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel     *naTitle;
@property (nonatomic, strong) UITextField *textFiled;

@end

@implementation ForgetEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = SNCOLORWHITE;
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20 + SNIPXTOPHEIGHT);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = SNFRAME(0, 20 + SNIPXTOPHEIGHT, kSCREEN_WIDTH, 44);
    naView.backgroundColor = WELLGOCOLORWHITE;
    [self.view addSubview:naView];
    naView.layer.shadowColor   = WELLGOCOLORBLACK.CGColor;//shadowColor阴影颜色
    naView.layer.shadowOffset  = CGSizeMake(1, 1);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    naView.layer.shadowOpacity = 0.5;//阴影透明度，默认0
    naView.layer.shadowRadius  = 1;//阴影半径，默认3
    
    UIImageView *image = [[UIImageView alloc] init];
    image.frame = CGRectMake(5, 20 + SNIPXTOPHEIGHT, 44, 44);
    image.image = [UIImage image:@"进入"];
    [self.view addSubview:image];
    
    UIButton *backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(0, 20+FitSimpleIpx(0, 20), 70, 44);
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    _naTitle               = [[UILabel alloc] initWithFrame:SNFRAME(0, 0, HHHWIDTH, 20)];
    _naTitle.center        = CGPointMake(HHHWIDTH/2, naView.centerY);
    _naTitle.text          = SN_Text(@"忘记密码?");
    _naTitle.font          = SNSFONT(17.0);
    _naTitle.textAlignment = NSTextAlignmentCenter;
    _naTitle.textColor     = WELLGOCOLORBLACK;
    [self.view addSubview:_naTitle];
    
    UILabel *infoLabel = [[UILabel alloc] init];
    infoLabel.frame         = SNFRAME(50, self.view.centerY - 50, screenWidth - 100, 20);
    infoLabel.textAlignment = NSTextAlignmentLeft;
    infoLabel.font          = SNFONT(16.0);
    infoLabel.textColor     = SNCOLORBLACK;
    infoLabel.text          = SN_Text(@"请输入注册邮箱");
    [self.view addSubview:infoLabel];
    
    _textFiled               = [[UITextField alloc] init];
    _textFiled.frame         = SNFRAME(50, self.view.centerY, screenWidth - 100, 30);
    _textFiled.textAlignment = NSTextAlignmentCenter;
    [_textFiled changePlaceholder:SN_Text(@"填写你的邮箱") withColor:HHHFONTCOLOR withFont:17.0];
    _textFiled.textColor = WELLGOCOLORBLACK;
    _textFiled.delegate  = self;
    [self.view addSubview:_textFiled];
    
    UIView *textLine = [[UIView alloc] init];
    textLine.backgroundColor = [UIColor colorFromHex:0xa9a9a9];
    textLine.frame           = SNFRAME(50, _textFiled.bottom + 3, _textFiled.width, 1.5);
    [self.view addSubview:textLine];
    
    UIButton *nextButton = [[UIButton alloc] init];
    nextButton.frame = SNFRAME(45, HHHHEIGHT - 75, HHHWIDTH - 90, 35);
    [nextButton setTitle:SN_Text(@"下一步") forState:UIControlStateNormal];
    nextButton.backgroundColor = SNCOLORBLACK;
    [nextButton setTitleColor:SNCOLORWHITE forState:UIControlStateNormal];
    [nextButton setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    nextButton.layer.cornerRadius = 5;
    [nextButton addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
}

- (void)nextButtonClick {
    if (![self isValidateEmail:_textFiled.text]) {
        SHOWMBProgressHUD(nil, SN_Text(@"邮件格式不正确"), nil, NO, 2.0);
        return;
    }
    [LOGINACCOUNT setObjectValue:_textFiled.text];
    [SERCRETEMAIL setObjectValue:_textFiled.text];
    
    [SECREETTYPE setBOOLValue:NO];
    SHOWMBProgressHUDIndeterminate(SN_Text(@"请等待..."), nil, @"登录中");
    [SN_Net getEmailSecretsuccess:^(id object) {
        [self delayLoad];
        if (object[@"ret"] && [object[@"ret"] intValue] == 0) {
            NSDictionary *dic = [object objectForKey:@"data"];
            NSLog(@"dic>>>>>>>%@", dic);
            NSInteger is_set = [dic[@"is_set"] integerValue];
            NSArray *array = dic[@"question"];
            if (is_set) {
                [SECREETTYPE setBOOLValue:YES];
                [SERCRET1 setObjectValue:[array firstObject]];
                [SERCRET2 setObjectValue:[array lastObject]];
            } else {
                [SECREETTYPE setBOOLValue:NO];
            }
            ForgetViewController *vc = [[ForgetViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            SHOWMBProgressHUD(nil,SN_Text(@"获取密保失败"), nil, NO, 2.0);
        }
    } fail:^(id object) {
        SHOWMBProgressHUD(nil,SN_Text(@"请重试"), nil, nil, NO);
        [self delayLoad];
    }];
    [self performSelector:@selector(delayLoad) withObject:nil afterDelay:30.0];
}

- (void)viewWillDisappear:(BOOL)animated {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayLoad) object:nil];
    [LOGINACCOUNT setObjectValue:@""];
    [LOGINPASSWORD setObjectValue:@""];
    [LOGINCODE setObjectValue:@""];
    HIDDENMBProgressHUD;
}

- (void)delayLoad {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayLoad) object:nil];
    HIDDENMBProgressHUD;
}

- (void)dealloc
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayLoad) object:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)isValidateEmail:(NSString *)email {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:email];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
